//
//  UIView+Simple.m
//  AJBracelet
//
//  Created by zorro on 15/5/26.
//  Copyright (c) 2015年 zorro. All rights reserved.
//

#import "UIView+public.h"
#import "NSObject+XMExchange.h"
#import <objc/runtime.h>

@implementation UIView (SimpleSwizzling)

+ (void)load {
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        [self exchangeClassMethod:@selector(initWithFrame:) newSel:@selector(pb_initWithFrame:)];
//        [self swizzleSelector:@selector(initWithFrame:) withSwizzledSelector:@selector(pb_initWithFrame:)];
//        [self swizzleSelector:@selector(setCenter:) withSwizzledSelector:@selector(pb_setCenter:)];
//        [self swizzleSelector:@selector(setFrame:) withSwizzledSelector:@selector(pb_setFrame:)];
    });
}

///** 自定义的方法 */
//- (void)pb_setBackgroundColor:(UIColor *)color {
//    NSLog(@"%s", __FUNCTION__);
//
//    [self pb_setBackgroundColor:[UIColor orangeColor]];
//}

- (void)pb_setFrame:(CGRect)frame
{
    CGFloat a = frame.origin.x;
    CGFloat b = frame.origin.y;
    CGFloat c = frame.size.width;
    CGFloat d = frame.size.height;
    
    if (isnan(a)) {
        a = 0.0;
    }
    if (isnan(b)) {
        b = 0.0;
    }
    if (isnan(c)) {
        c = 0.0;
    }
    if (isnan(d)) {
        d = 0.0;
    }
    CGRect rect = CGRectMake(a, b, c, d);
    [self pb_setFrame:rect];
//    NSLog(@"pb_setFrame: %s", __FUNCTION__);
}

- (void)pb_setCenter:(CGPoint)center
{
//    NSLog(@"setCenter:%s", __FUNCTION__);
    CGFloat a = center.x;
    CGFloat b = center.y;
    if (isnan(a)) {
        a = 0.0;
    }
    if (isnan(b)) {
        b = 0.0;
    }
    CGPoint p = CGPointMake(a, b);
    [self pb_setCenter:p];
}

- (void)pb_initWithFrame:(CGRect)frame {
    CGFloat a = frame.origin.x;
    CGFloat b = frame.origin.y;
    CGFloat c = frame.size.width;
    CGFloat d = frame.size.height;
    
    if (isnan(a)) {
        a = 0.0;
    }
    if (isnan(b)) {
        b = 0.0;
    }
    if (isnan(c)) {
        c = 0.0;
    }
    if (isnan(d)) {
        d = 0.0;
    }
    CGRect rect = CGRectMake(a, b, c, d);
//    NSLog(@"initWithFrame:%s", __FUNCTION__);
    [self pb_initWithFrame:rect];
}

@end
